package com.example.demo.controller;
import com.example.demo.Common.CommonIT;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.annotation.AuthToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.demo.Excel.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xc.roadRisk.common.config.SwaggerAutoConfig;
import com.xc.roadRisk.common.utils.ResponseUtils;
import com.xc.roadRisk.common.vo.Response;
import com.xc.roadRisk.common.vo.PageList;

import java.util.Date;
import java.util.List;
import com.example.demo.entity.Dynamic_results;
import com.example.demo.service.IDynamic_resultsService;

 /**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hanfei
 * @since 2021-05-13
 */
@Api(value = "设备动态",tags = "设备动态")
@RestController
public class Dynamic_resultsController {
    @Autowired
    private IDynamic_resultsService dynamic_resultsApi;
    @AuthToken
    @ApiOperation("创建单个Dynamic_results")
    @ApiImplicitParam(name = "version", paramType = "path")
    @PostMapping("pv/dynamic_resultss")
    public Response<Boolean> insert(@RequestBody Dynamic_results dynamic_results) {
        CommonIT commonIT=new CommonIT();
        String id=commonIT.findId();
        dynamic_results.setDynamic_id(id);
        dynamic_results.setCreate_time(new Date());
        Response<Boolean> response=ResponseUtils.returnObjectSuccess(dynamic_resultsApi.insert(dynamic_results));
        response.setMsg(id);
        return response;
    }
    @AuthToken
    @ApiOperation("编辑单个Dynamic_results")
    @ApiImplicitParam(name = "version", paramType = "path")
    @PutMapping("pv/dynamic_resultss/{uid}")
    public Response<Boolean> updateById(@PathVariable("uid") String uid, @RequestBody Dynamic_results dynamic_results) {
        dynamic_results.setDynamic_id(uid);
     dynamic_results.setUpdate_time(new Date());
        return ResponseUtils.returnObjectSuccess(dynamic_resultsApi.updateById(dynamic_results));
    }
    @AuthToken
    @ApiOperation("查询单个Dynamic_results")
    @ApiImplicitParam(name = "version", paramType = "path")
    @GetMapping("pb/dynamic_resultss/{uid}")
    public Response<Dynamic_results> selectById(@PathVariable("uid") String uid) {
        return ResponseUtils.returnObjectSuccess(dynamic_resultsApi.selectById(uid));
    }

    @AuthToken
    @ApiOperation("查询所有")
    @ApiImplicitParam(name = "version", paramType = "path")
    @GetMapping("pb/dynamic_resultss/findall")
    public Response<List<Dynamic_results>> findall(Dynamic_results dynamic_results) {
        return ResponseUtils.returnObjectSuccess(dynamic_resultsApi.selectList(new EntityWrapper<>(dynamic_results)));
        }
     @AuthToken
    @ApiOperation("导出")
    @GetMapping("/dynamic_resultss/export")
    public String exportAll(HttpServletResponse response,Dynamic_results dynamic_results) {
        List<Dynamic_results> list=dynamic_resultsApi.selectList(new EntityWrapper<>(dynamic_results));
        ExcelUtils.writeExcel(response,list,Dynamic_results.class);
        return null;
        }
    @AuthToken
    @ApiOperation("导入")
    @PostMapping("/dynamic_resultss/import")
    public Response importAll(@RequestParam("file")MultipartFile file){
        List<Dynamic_results> list=ExcelUtils.readExcel(null,Dynamic_results.class,file);
        for(Dynamic_results a:list){
        CommonIT commonIT=new CommonIT();
        String id=commonIT.findId();
        a.setDynamic_id(id);
        a.setCreate_time(new Date());
        }

        return ResponseUtils.returnObjectSuccess(dynamic_resultsApi.insertBatch(list));
        }
    @AuthToken
    @ApiOperation("删除单个Dynamic_results")
    @ApiImplicitParam(name = "version", paramType = "path")
    @DeleteMapping("delete/dynamic_resultss/{uid}")
    public Response<Boolean> deleteById(@PathVariable("uid") String uid) {

        return ResponseUtils.returnObjectSuccess(dynamic_resultsApi.deleteById(uid));
        }
        @AuthToken
        @ApiOperation("模糊查询加定向查询分页")
        @ApiImplicitParam(name = "version", paramType = "path")
        @GetMapping("pb/dynamic_resultss/action/findByKeyAndAll")
        public Response<PageList<Dynamic_results>> findByKeyAndAll(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "key",required = false) String key,
              Dynamic_results entity) {
        Page<Dynamic_results> page = dynamic_resultsApi.findByKeyAndAll(new Page<>(pageNo, pageSize), key,entity);
        return ResponseUtils.returnObjectSuccess(new PageList<>(page));
        }
}
